package com.ld.collegetask;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.google.gson.Gson;
import com.ld.collegetask.beans.BaseJsonBean;
import com.ld.collegetask.databinding.ActivityMemberForgetPasswordResetBinding;
import com.ld.collegetask.login.LoginActivity;
import com.ld.collegetask.serverInterface.RequestSerives;
import com.ld.collegetask.utils.AppApplication;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MemberForgetPasswordResetActivity extends BaseActivity {
    private ActivityMemberForgetPasswordResetBinding binding;
    private Integer userId = 0;
    private String username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_member_forget_password_reset);

        //userId = Integer.parseInt(getIntent().getStringExtra("userId"));
        setTitleView(this,binding.ilTitle,true,true,false,false);
        setLoadingView(this,binding.ilLoading);
        setTitle("重置密码");
        username = getIntent().getStringExtra("username");

        findView();
    }

    @Override
    public void loading() {

    }

    public void findView() {
        binding.memberForgetPasswordResetButton.setOnClickListener(this);
        binding.memberForgetPasswordUserPassword.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
                if (s.toString().trim().length() > 0) {
                    binding.memberForgetPasswordResetButton.setBackgroundColor(Color.parseColor("#40D15C"));
                    binding.memberForgetPasswordResetButton.setEnabled(true);
                } else {
                    binding.memberForgetPasswordResetButton.setBackgroundColor(Color.parseColor("#CCCCCC"));
                    binding.memberForgetPasswordResetButton.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private void member_username_unique(String password) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.member_reset_password(username,password);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    startActivity(new Intent(MemberForgetPasswordResetActivity.this, LoginActivity.class));
                    finish();
                } else {

                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.member_forget_password_reset_button:
                String password = binding.memberForgetPasswordUserPassword.getText().toString();
                if (!"".equals(password)) {
                    member_username_unique(password);
                } else {

                }
                break;
        }
    }
}
